package com.zhsq.zlc.service.authority;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhsq.zlc.dao.authority.AuthorityMapper;
import com.zhsq.zlc.entity.AuthorityEntity;
import com.zhsq.zlc.entity.AuthorityTreeEntity;
import com.zhsq.zlc.entity.UserAuthorityEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AuthorityServiceImpl implements AuthorityService {

	@Autowired
	private AuthorityMapper authorityMapper;

	@Override
	public List<AuthorityEntity> list(AuthorityEntity entity) throws Exception {
		return authorityMapper.list(entity);
	}

	@Override
	public int count(AuthorityEntity entity) throws Exception {
		return authorityMapper.count(entity);
	}

	@Override
	public AuthorityEntity getAuthorityById(AuthorityEntity entity) throws Exception {
		return authorityMapper.getAuthorityById(entity).get(0);
	}

	@Override
	public void add(AuthorityEntity entity) throws Exception {
		authorityMapper.add(entity);		
	}

	@Override
	public void edit(AuthorityEntity entity) throws Exception {
		authorityMapper.edit(entity);		
	}

	@Override
	public void delete(int id) throws Exception {
		authorityMapper.delete(id);		
	}

	@Override
	public List<AuthorityTreeEntity> getAuthorityTree() throws Exception {
		List<AuthorityTreeEntity> tree =new ArrayList<AuthorityTreeEntity>();
		List<AuthorityEntity> list = authorityMapper.getAuthorityTree();
		if(list!=null && list.size()>0){
			for(AuthorityEntity parent: list){
				if(parent.getParentId()!=null && parent.getParentId()>0){}else{
					AuthorityTreeEntity at=new AuthorityTreeEntity();
					at.setId(parent.getId());
					at.setParentId(parent.getParentId());
					at.setText(parent.getName());
					at.addNodes(getTreeChildren(parent.getId(),list));
					tree.add(at);
				}
			}
		}
		return tree;
	}

	@Override
	public AuthorityEntity getParentAuthorityById(Integer roleId) throws Exception {
		return authorityMapper.getParentAuthorityById(roleId).get(0);
	}

	private List<AuthorityTreeEntity> getTreeChildren(Integer parentId,List<AuthorityEntity> list)throws Exception{
		List<AuthorityTreeEntity> treeList = new ArrayList<AuthorityTreeEntity>();
		for(AuthorityEntity child: list){
			if(child.getParentId()!=null && child.getParentId()>0 && child.getParentId()==parentId){
				AuthorityTreeEntity treeChild=new AuthorityTreeEntity();
				treeChild.setId(child.getId());
				treeChild.setParentId(child.getParentId());
				treeChild.setText(child.getName());
				treeChild.addNodes(getTreeChildren(child.getId(),list));
				treeList.add(treeChild);
			}
		}
		return treeList;
	}
	
	@Override
	public List<AuthorityEntity> getAuthoritysByParentId(Integer parentId) throws Exception {
		return authorityMapper.getAuthoritysByParentId(parentId);
	}

	@Override
	public List<AuthorityEntity> getAuthorityByRoleId(Integer roleId) throws Exception {
		List<AuthorityEntity> list = authorityMapper.getAuthorityByRoleId(roleId);
		return list;

	}

	@Override
	public List<UserAuthorityEntity> getAuthorityTreeByRoleId(Integer roleId) throws Exception {
		List<UserAuthorityEntity> tree =new ArrayList<UserAuthorityEntity>();
		List<AuthorityEntity> list = authorityMapper.getAuthorityByRoleId(roleId);
		if(list!=null && list.size()>0){
			for(AuthorityEntity parent: list){
				if(parent.getParentId()!=null && parent.getParentId()>0){}else{
					UserAuthorityEntity uae=new UserAuthorityEntity();
					uae.setId(parent.getId());
					uae.setName(parent.getName());
					uae.setUrl(parent.getUrl());
					uae.setParentId(parent.getParentId());
					uae.setParentName(parent.getParentName());
					uae.setIsHeader(1);
					uae.setSortNumber(parent.getSortNumber());
					//uae.setIcon("fa-"+parent.getIcon());
					uae.setIcon(parent.getIcon());
					uae.setDescription(parent.getDescription());
					uae.addChildMenus(getUATreeChildren(parent.getId(),list));
					tree.add(uae);
				}
			}
		}
		return tree;
		
	}
	private List<UserAuthorityEntity> getUATreeChildren(Integer parentId,List<AuthorityEntity> list)throws Exception{
		List<UserAuthorityEntity> treeList = new ArrayList<UserAuthorityEntity>();
		for(AuthorityEntity child: list){
			if(child.getParentId()!=null && child.getParentId()>0 && child.getParentId()==parentId){
				UserAuthorityEntity uae=new UserAuthorityEntity();
				uae.setId(child.getId());
				uae.setName(child.getName());
				uae.setUrl(child.getUrl());
				uae.setParentId(child.getParentId());
				uae.setParentName(child.getParentName());
				uae.setIsHeader(0);
				uae.setSortNumber(child.getSortNumber());
				//uae.setIcon("fa-"+child.getIcon());
				uae.setIcon(child.getIcon());
				uae.setDescription(child.getDescription());
				uae.addChildMenus(getUATreeChildren(child.getId(),list));
				treeList.add(uae);
			}
		}
		return treeList;
	}

	@Override
	public void delAuthorityByRole(Integer roleId) throws Exception {
		authorityMapper.delAuthorityByRole(roleId);
	}

	@Override
	public void batchInsertRoleAuthority(Integer roleId, String authorityIds) throws Exception {
		List<Map<String, Object>> param = new ArrayList<Map<String, Object>>();
		if (authorityIds!=null && !"".equals(authorityIds.trim())){
			for (String authorityId : authorityIds.split(",")){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("roleId", roleId);
				map.put("authorityId", authorityId);
				param.add(map);
			}
			authorityMapper.batchInsertRoleAuthortity(param);
		}
	}
}
